﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace WSAP
{
    public static class WSAPBeamLoad
    {
        /// <summary>
        /// 均布荷载测试
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="length"></param>
        /// <param name="gravityUniformLoad"></param>
        public static bool UniformLoad(double width, double height, double length,
            double beamEndLength,
            double gravityUniformLoad)
        {
            //获得模型
            var model = GetWModelNet(width, height, length, beamEndLength);
            //遍历单元
            model.GetElements().ForEach(element => 
            model.AddBeamUniformLoad(element.GetTag(), gravityUniformLoad));
            //创建有限元模型
            if (!model.CreateFEModel(false)) return model.ShowWarningError();
            //求解
            if (!model.DoLinearStaticAnalyze()) return model.ShowWarningError();
            //绘制内力图
            model.GetElements().ForEach(element => element.SectionForceToChart());
            return true;
        }

        /// <summary>
        /// 点荷载测试
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="length"></param>
        /// <param name="gravityUniformLoad"></param>
        public static bool PointLoad(double width, double height, double length,
            double beamEndLength,
            double gravityLoad, double loadLengthFacotr)
        {
            //获得模型
            var model = GetWModelNet(width, height, length, beamEndLength);
            //遍历单元
            model.GetElements().ForEach(element =>
            model.AddBeamPointLoad(element.GetTag(), gravityLoad, loadLengthFacotr));
            //创建有限元模型
            if (!model.CreateFEModel(false)) return model.ShowWarningError();
            //求解
            if (!model.DoLinearStaticAnalyze()) return model.ShowWarningError();
            //绘制内力图
            model.GetElements().ForEach(element => element.SectionForceToChart());
            return true;
        }

        /// <summary>
        /// 三角荷载测试
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="length"></param>
        /// <param name="gravityUniformLoad"></param>
        public static bool TriangularLoad(double width, double height, double length,
            double beamEndLength,
            double gravityLoad, double loadLengthFacotr)
        {
            //获得模型
            var model = GetWModelNet(width, height, length, beamEndLength);
            //遍历单元
            model.GetElements().ForEach(element =>
            model.AddBeamTriangularLoad(element.GetTag(), gravityLoad, loadLengthFacotr));
            //创建有限元模型
            if (!model.CreateFEModel(false)) return model.ShowWarningError();
            //求解
            if (!model.DoLinearStaticAnalyze()) return model.ShowWarningError();
            //绘制内力图
            model.GetElements().ForEach(element => element.SectionForceToChart());
            return true;
        }

        /// <summary>
        /// 梯形荷载测试
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="length"></param>
        /// <param name="gravityUniformLoad"></param>
        public static bool TrapezoidLoad(double width, double height, double length,
             double beamEndLength,
            double gravityMaxLoad, double maxLoadLengthFacotrI, double maxLoadLengthFacotrJ)
        {
            //获得模型
            var model = GetWModelNet(width, height, length, beamEndLength);
            //遍历单元
            model.GetElements().ForEach(element =>
            model.AddBeamTrapezoidLoad(element.GetTag(), gravityMaxLoad,
            maxLoadLengthFacotrI, maxLoadLengthFacotrJ));
            //创建有限元模型
            if (!model.CreateFEModel(false)) return model.ShowWarningError();
            //求解
            if (!model.DoLinearStaticAnalyze()) return model.ShowWarningError();
            //绘制内力图
            model.GetElements().ForEach(element => element.SectionForceToChart());
            return true;
        }

        /// <summary>
        /// 建立算例有限元模型
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        private static WModelNet GetWModelNet(double width, 
            double height, double length, double beamEndLength)
        {
            //初始化模型参数
            var model = new WModelNet();
            //添加节点
            model.AddNode(0, 0, 0, 0, true);
            model.AddNode(1, length, 0, 0, true);
            model.AddNode(2, 0, 3000, 1E3, true);
            model.AddNode(3, length, 3000, 1E3, true);
            model.AddNode(4, 0, 6000, 0, true);
            model.AddNode(5, length, 6000, 0, true);
            //添加梁单元
            var elementI = model.AddRectConcreteBeam(0, 1, width, height);
            var elementJ = model.AddRectConcreteBeam(2, 3, width, height);
            var elementK = model.AddRectConcreteBeam(4, 5, width, height);
            //二号单元左端简支 三号单元全简支
            elementJ.SetReleaseType(FrameReleaseEnum.IRelease);
            elementK.SetReleaseType(FrameReleaseEnum.IJRelease);
            //尝试增加刚域
            if(beamEndLength != 0)
            {
                elementI.SetRigidEndLength(beamEndLength, beamEndLength);
                elementJ.SetRigidEndLength(beamEndLength, beamEndLength);
                elementK.SetRigidEndLength(beamEndLength, beamEndLength);
            }
            //返回模型
            return model;
        }
    }
}
